<?php
require('inc/init.inc.php');

class FluxRss{
	
var $db;
var $sql;
var $channelattributs;
var $itemattributs;
var $items;
var $xml;
var $pas;

public function __construct($db, $sql, $title, $link, $description){
	$this->db = $db;
	$this->sql = $sql;
	$this->channelattribut = Array();
	$this->itemattribut = Array();
	$this->items = Array();
	$this->xml='';
	$this->SetChannelAttribut('title',$title);
	$this->SetChannelAttribut('link',$link);
	$this->SetChannelAttribut('description',$description);
}

public function SetChannelAttribut($attribut, $valeur){
	$this->channelattributs[$attribut] = $valeur;
}

public function SetItemAttribut($attribut, $valeur, $format){
	$this->itemattributs[$attribut] = Array($valeur, $format);
}

private function OuvrirBalise($balise, $ouvrir, &$indent){
	$indent += ($ouvrir)? 1: -1;
	$c = ($ouvrir)? '': '/';
	$this->Ecrire(str_repeat(' ',$indent*$this->pas)."<$c$balise>");
	$indent += ($ouvrir)? 1: -1;
}

private function EcrireBalise($balise, $valeur, $indent){
	$this->Ecrire(str_repeat(' ',$indent*$this->pas)."<$balise>$valeur</$balise>");
}

private function Ecrire($texte){
	$this->xml .= "$texte\n";
}

public function OutPut($pas=4){
	$this->pas = $pas;
	$indent=0;
	$this->Ecrire("<rss version='2.0'>");
	$this->OuvrirBalise('channel', true, $indent);
	foreach	($this->channelattributs as $attribut=>$valeur)
		$this->EcrireBalise($attribut, $valeur, $indent);
	if ($result = $this->db->query($this->sql))
		while ($row = $result->fetch_assoc()){
			$this->OuvrirBalise('item', true, $indent);
			foreach	($this->itemattributs as $attribut=>$valeur)
				$this->EcrireBalise($attribut, sprintf($valeur[1],$row[$valeur[0]]), $indent);
			$this->OuvrirBalise('item', false, $indent);
		}	
	$this->OuvrirBalise('channel', false, $indent);
	$this->Ecrire("</rss>");
	return $this->xml;
}
}//class FluxRss
	
$sql = "SELECT CONCAT(rap_objet, ' (', cli_nom, ') par ', CONCAT_WS(', ', uti_nom, uti_prenom)) as titre, rap_id as id, rap_date
		FROM rapports
		JOIN clients ON clients.cli_id = rapports.rap_cli_id
		JOIN utilisateurs ON utilisateurs.uti_id = rapports.rap_uti_id
		ORDER BY rap_date
		LIMIT 0, 10";

/*
$rss = new FluxRss($db, $sql, 'Actualités', '', 'Les derniers rapports publiés');
$rss->SetItemAttribut('title', 'titre', "%s");
$rss->SetItemAttribut('link', 'id', "rapport.php?id=%s");
echo $rss->OutPut();
*/

?>
